<?php
/**
 * @file promotion.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015年9月7日 下午14:19:50 $
 * @brief app 推广平台数据列表 $
 */
namespace haibao\app\business;

class Promotion extends \haibao\app\business\Base {
	
	/**
	 * @var $_instance
	 */
	private static $_instance;
	
	/**
	 * instance()
	 */
	private static function instance(){
		if(!self::$_instance){
			self::$_instance = new \haibao\app\data\Promotion();
		}
		return self::$_instance;
	}
	
	/**
	 * 获取所有推广平台数据
	 * @param unknown $query
	 */
	public static function getAppExtensionCount($query){
		return self::instance()->getAll($query);
	}
	
	/**
	 * 添加推广平台数据
	 */
	public static function addPromotion($model){
		self::checkOperation(self::FUNC_CODE_APP_SPREAD_ADD);
		self::checkPromotionModel($model);
		return self::instance()->addPromotion($model);
		self::saveOperateHistory(self::FUNC_CODE_APP_SPREAD_ADD, $model->toArray(true,true));
	}
	
	/**
	 * 删除推广平台数据
	 */
	public static function updateProStatus($id){
		self::checkOperation(self::FUNC_CODE_APP_SPREAD_DELETE);
		self::instance()->updateProStatus($id);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = \haibao\app\model\data\Promotion::STATUS_DISABLE;
		self::saveOperateHistory(self::FUNC_CODE_APP_SPREAD_DELETE,$arrSet);
	}
	
	public static function getPromotion($platform,$selectType){
		return self::instance()->getPromotion($platform,$selectType);
	}
	
	/**
	 * 添加推广点击记录
	 */
	public static function addPromotionRecord($proId, $ClickIP){
		self::instance()->addPromotionRecord($proId, $ClickIP);
	}
	
	/**
	 * 得到推广平台名称
	 */
	public static function getPromotionByName($name){
		return self::instance()->getPromotionByName($name);
	}
	
	/**
	 * 验证新增监测
	 */
	private static function checkPromotionModel($model){
		if(!$model->ProName){
			throw new BusinessException('平台名称不能为空');
		}
		if($model->DownloadType == ''){
			throw new BusinessException('应用类型不能为空');
		}
		$result = self::instance()->getPromotion($model->ProName,$model->DownloadType);
		if(!empty($result)){
			throw new BusinessException('平台名称重复');
		}
	}
	
	
	
}
